﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnxc.DBUtility;//Please add references
namespace jnxc.DAL
{
	/// <summary>
	/// 数据访问类:addhappen
	/// </summary>
	public partial class addhappen
	{
		public addhappen()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("addhappen_id", "addhappen");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int addhappen_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from addhappen");
            strSql.Append(" where addhappen_id=@addhappen_id");
            SqlParameter[] parameters = {
					new SqlParameter("@addhappen_id", SqlDbType.Int,4)
			};
            parameters[0].Value = addhappen_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(jnxc.Model.addhappen model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into addhappen(");
            strSql.Append("aid,dpid,trees,jzarea,jcarea,yjarea,generation,dctime,fbarea,dckdgbarea,jcfgarea,generationfsarea,ljqd,ljzd,ljzzd,fshj,fsqd,fszd,fszzd,tbcxfs,fsl,yjczarea,uid,nd,yf)");
            strSql.Append(" values (");
            strSql.Append("@aid,@dpid,@trees,@jzarea,@jcarea,@yjarea,@generation,@dctime,@fbarea,@dckdgbarea,@jcfgarea,@generationfsarea,@ljqd,@ljzd,@ljzzd,@fshj,@fsqd,@fszd,@fszzd,@tbcxfs,@fsl,@yjczarea,@uid,@nd,@yf)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@dpid", SqlDbType.Int,4),
					new SqlParameter("@trees", SqlDbType.NVarChar,20),
					new SqlParameter("@jzarea", SqlDbType.Decimal,9),
					new SqlParameter("@jcarea", SqlDbType.Decimal,9),
					new SqlParameter("@yjarea", SqlDbType.Decimal,9),
					new SqlParameter("@generation", SqlDbType.NChar,10),
					new SqlParameter("@dctime", SqlDbType.DateTime),
					new SqlParameter("@fbarea", SqlDbType.Decimal,9),
					new SqlParameter("@dckdgbarea", SqlDbType.Decimal,9),
					new SqlParameter("@jcfgarea", SqlDbType.Decimal,9),
					new SqlParameter("@generationfsarea", SqlDbType.Decimal,9),
					new SqlParameter("@ljqd", SqlDbType.Decimal,9),
					new SqlParameter("@ljzd", SqlDbType.Decimal,9),
					new SqlParameter("@ljzzd", SqlDbType.Decimal,9),
					new SqlParameter("@fshj", SqlDbType.Decimal,9),
					new SqlParameter("@fsqd", SqlDbType.Decimal,9),
					new SqlParameter("@fszd", SqlDbType.Decimal,9),
					new SqlParameter("@fszzd", SqlDbType.Decimal,9),
					new SqlParameter("@tbcxfs", SqlDbType.Decimal,9),
					new SqlParameter("@fsl", SqlDbType.Decimal,9),
					new SqlParameter("@yjczarea", SqlDbType.Decimal,9),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@nd", SqlDbType.Int,4),
					new SqlParameter("@yf", SqlDbType.Int,4)};
            parameters[0].Value = model.aid;
            parameters[1].Value = model.dpid;
            parameters[2].Value = model.trees;
            parameters[3].Value = model.jzarea;
            parameters[4].Value = model.jcarea;
            parameters[5].Value = model.yjarea;
            parameters[6].Value = model.generation;
            parameters[7].Value = model.dctime;
            parameters[8].Value = model.fbarea;
            parameters[9].Value = model.dckdgbarea;
            parameters[10].Value = model.jcfgarea;
            parameters[11].Value = model.generationfsarea;
            parameters[12].Value = model.ljqd;
            parameters[13].Value = model.ljzd;
            parameters[14].Value = model.ljzzd;
            parameters[15].Value = model.fshj;
            parameters[16].Value = model.fsqd;
            parameters[17].Value = model.fszd;
            parameters[18].Value = model.fszzd;
            parameters[19].Value = model.tbcxfs;
            parameters[20].Value = model.fsl;
            parameters[21].Value = model.yjczarea;
            parameters[22].Value = model.uid;
            parameters[23].Value = model.nd;
            parameters[24].Value = model.yf;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(jnxc.Model.addhappen model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update addhappen set ");
            strSql.Append("aid=@aid,");
            strSql.Append("dpid=@dpid,");
            strSql.Append("trees=@trees,");
            strSql.Append("jzarea=@jzarea,");
            strSql.Append("jcarea=@jcarea,");
            strSql.Append("yjarea=@yjarea,");
            strSql.Append("generation=@generation,");
            strSql.Append("dctime=@dctime,");
            strSql.Append("fbarea=@fbarea,");
            strSql.Append("dckdgbarea=@dckdgbarea,");
            strSql.Append("jcfgarea=@jcfgarea,");
            strSql.Append("generationfsarea=@generationfsarea,");
            strSql.Append("ljqd=@ljqd,");
            strSql.Append("ljzd=@ljzd,");
            strSql.Append("ljzzd=@ljzzd,");
            strSql.Append("fshj=@fshj,");
            strSql.Append("fsqd=@fsqd,");
            strSql.Append("fszd=@fszd,");
            strSql.Append("fszzd=@fszzd,");
            strSql.Append("tbcxfs=@tbcxfs,");
            strSql.Append("fsl=@fsl,");
            strSql.Append("yjczarea=@yjczarea,");
            strSql.Append("uid=@uid,");
            strSql.Append("nd=@nd,");
            strSql.Append("yf=@yf");
            strSql.Append(" where addhappen_id=@addhappen_id");
            SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@dpid", SqlDbType.Int,4),
					new SqlParameter("@trees", SqlDbType.NVarChar,20),
					new SqlParameter("@jzarea", SqlDbType.Decimal,9),
					new SqlParameter("@jcarea", SqlDbType.Decimal,9),
					new SqlParameter("@yjarea", SqlDbType.Decimal,9),
					new SqlParameter("@generation", SqlDbType.NChar,10),
					new SqlParameter("@dctime", SqlDbType.DateTime),
					new SqlParameter("@fbarea", SqlDbType.Decimal,9),
					new SqlParameter("@dckdgbarea", SqlDbType.Decimal,9),
					new SqlParameter("@jcfgarea", SqlDbType.Decimal,9),
					new SqlParameter("@generationfsarea", SqlDbType.Decimal,9),
					new SqlParameter("@ljqd", SqlDbType.Decimal,9),
					new SqlParameter("@ljzd", SqlDbType.Decimal,9),
					new SqlParameter("@ljzzd", SqlDbType.Decimal,9),
					new SqlParameter("@fshj", SqlDbType.Decimal,9),
					new SqlParameter("@fsqd", SqlDbType.Decimal,9),
					new SqlParameter("@fszd", SqlDbType.Decimal,9),
					new SqlParameter("@fszzd", SqlDbType.Decimal,9),
					new SqlParameter("@tbcxfs", SqlDbType.Decimal,9),
					new SqlParameter("@fsl", SqlDbType.Decimal,9),
					new SqlParameter("@yjczarea", SqlDbType.Decimal,9),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@nd", SqlDbType.Int,4),
					new SqlParameter("@yf", SqlDbType.Int,4),
					new SqlParameter("@addhappen_id", SqlDbType.Int,4)};
            parameters[0].Value = model.aid;
            parameters[1].Value = model.dpid;
            parameters[2].Value = model.trees;
            parameters[3].Value = model.jzarea;
            parameters[4].Value = model.jcarea;
            parameters[5].Value = model.yjarea;
            parameters[6].Value = model.generation;
            parameters[7].Value = model.dctime;
            parameters[8].Value = model.fbarea;
            parameters[9].Value = model.dckdgbarea;
            parameters[10].Value = model.jcfgarea;
            parameters[11].Value = model.generationfsarea;
            parameters[12].Value = model.ljqd;
            parameters[13].Value = model.ljzd;
            parameters[14].Value = model.ljzzd;
            parameters[15].Value = model.fshj;
            parameters[16].Value = model.fsqd;
            parameters[17].Value = model.fszd;
            parameters[18].Value = model.fszzd;
            parameters[19].Value = model.tbcxfs;
            parameters[20].Value = model.fsl;
            parameters[21].Value = model.yjczarea;
            parameters[22].Value = model.uid;
            parameters[23].Value = model.nd;
            parameters[24].Value = model.yf;
            parameters[25].Value = model.addhappen_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int addhappen_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from addhappen ");
            strSql.Append(" where addhappen_id=@addhappen_id");
            SqlParameter[] parameters = {
					new SqlParameter("@addhappen_id", SqlDbType.Int,4)
			};
            parameters[0].Value = addhappen_id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string addhappen_idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from addhappen ");
            strSql.Append(" where addhappen_id in (" + addhappen_idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public jnxc.Model.addhappen GetModel(int addhappen_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 addhappen_id,aid,dpid,trees,jzarea,jcarea,yjarea,generation,dctime,fbarea,dckdgbarea,jcfgarea,generationfsarea,ljqd,ljzd,ljzzd,fshj,fsqd,fszd,fszzd,tbcxfs,fsl,yjczarea,uid,nd,yf from addhappen ");
            strSql.Append(" where addhappen_id=@addhappen_id");
            SqlParameter[] parameters = {
					new SqlParameter("@addhappen_id", SqlDbType.Int,4)
			};
            parameters[0].Value = addhappen_id;

            jnxc.Model.addhappen model = new jnxc.Model.addhappen();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public jnxc.Model.addhappen DataRowToModel(DataRow row)
        {
            jnxc.Model.addhappen model = new jnxc.Model.addhappen();
            if (row != null)
            {
                if (row["addhappen_id"] != null && row["addhappen_id"].ToString() != "")
                {
                    model.addhappen_id = int.Parse(row["addhappen_id"].ToString());
                }
                if (row["aid"] != null && row["aid"].ToString() != "")
                {
                    model.aid = int.Parse(row["aid"].ToString());
                }
                if (row["dpid"] != null && row["dpid"].ToString() != "")
                {
                    model.dpid = int.Parse(row["dpid"].ToString());
                }
                if (row["trees"] != null)
                {
                    model.trees = row["trees"].ToString();
                }
                if (row["jzarea"] != null && row["jzarea"].ToString() != "")
                {
                    model.jzarea = decimal.Parse(row["jzarea"].ToString());
                }
                if (row["jcarea"] != null && row["jcarea"].ToString() != "")
                {
                    model.jcarea = decimal.Parse(row["jcarea"].ToString());
                }
                if (row["yjarea"] != null && row["yjarea"].ToString() != "")
                {
                    model.yjarea = decimal.Parse(row["yjarea"].ToString());
                }
                if (row["generation"] != null)
                {
                    model.generation = row["generation"].ToString();
                }
                if (row["dctime"] != null && row["dctime"].ToString() != "")
                {
                    model.dctime = DateTime.Parse(row["dctime"].ToString());
                }
                if (row["fbarea"] != null && row["fbarea"].ToString() != "")
                {
                    model.fbarea = decimal.Parse(row["fbarea"].ToString());
                }
                if (row["dckdgbarea"] != null && row["dckdgbarea"].ToString() != "")
                {
                    model.dckdgbarea = decimal.Parse(row["dckdgbarea"].ToString());
                }
                if (row["jcfgarea"] != null && row["jcfgarea"].ToString() != "")
                {
                    model.jcfgarea = decimal.Parse(row["jcfgarea"].ToString());
                }
                if (row["generationfsarea"] != null && row["generationfsarea"].ToString() != "")
                {
                    model.generationfsarea = decimal.Parse(row["generationfsarea"].ToString());
                }
                if (row["ljqd"] != null && row["ljqd"].ToString() != "")
                {
                    model.ljqd = decimal.Parse(row["ljqd"].ToString());
                }
                if (row["ljzd"] != null && row["ljzd"].ToString() != "")
                {
                    model.ljzd = decimal.Parse(row["ljzd"].ToString());
                }
                if (row["ljzzd"] != null && row["ljzzd"].ToString() != "")
                {
                    model.ljzzd = decimal.Parse(row["ljzzd"].ToString());
                }
                if (row["fshj"] != null && row["fshj"].ToString() != "")
                {
                    model.fshj = decimal.Parse(row["fshj"].ToString());
                }
                if (row["fsqd"] != null && row["fsqd"].ToString() != "")
                {
                    model.fsqd = decimal.Parse(row["fsqd"].ToString());
                }
                if (row["fszd"] != null && row["fszd"].ToString() != "")
                {
                    model.fszd = decimal.Parse(row["fszd"].ToString());
                }
                if (row["fszzd"] != null && row["fszzd"].ToString() != "")
                {
                    model.fszzd = decimal.Parse(row["fszzd"].ToString());
                }
                if (row["tbcxfs"] != null && row["tbcxfs"].ToString() != "")
                {
                    model.tbcxfs = decimal.Parse(row["tbcxfs"].ToString());
                }
                if (row["fsl"] != null && row["fsl"].ToString() != "")
                {
                    model.fsl = decimal.Parse(row["fsl"].ToString());
                }
                if (row["yjczarea"] != null && row["yjczarea"].ToString() != "")
                {
                    model.yjczarea = decimal.Parse(row["yjczarea"].ToString());
                }
                if (row["uid"] != null && row["uid"].ToString() != "")
                {
                    model.uid = int.Parse(row["uid"].ToString());
                }
                if (row["nd"] != null && row["nd"].ToString() != "")
                {
                    model.nd = int.Parse(row["nd"].ToString());
                }
                if (row["yf"] != null && row["yf"].ToString() != "")
                {
                    model.yf = int.Parse(row["yf"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select addhappen_id,aid,dpid,trees,jzarea,jcarea,yjarea,generation,dctime,fbarea,dckdgbarea,jcfgarea,generationfsarea,ljqd,ljzd,ljzzd,fshj,fsqd,fszd,fszzd,tbcxfs,fsl,yjczarea,uid,nd,yf ");
            strSql.Append(" FROM addhappen ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" addhappen_id,aid,dpid,trees,jzarea,jcarea,yjarea,generation,dctime,fbarea,dckdgbarea,jcfgarea,generationfsarea,ljqd,ljzd,ljzzd,fshj,fsqd,fszd,fszzd,tbcxfs,fsl,yjczarea,uid,nd,yf ");
            strSql.Append(" FROM addhappen ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM addhappen ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
       

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "addhappen";
            parameters[1].Value = "addhappen_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.apid desc");
            }
            strSql.Append(")AS Row, T.*  from VaddHappen T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
		#endregion  ExtensionMethod
	}
}

